package com.qingyun.customview.hencode.practice1

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.qingyun.customview.R
import com.qingyun.customview.databinding.ActivityPractice1MainBinding

class Practice1MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityPractice1MainBinding

    //@formatter:off
    private val  pageModels = listOf(
        PageModel(R.layout.sample_color, R.string.title_draw_color, R.layout.practice_color),
        PageModel(R.layout.sample_circle, R.string.title_draw_circle, R.layout.practice_circle),
        PageModel(R.layout.sample_rect, R.string.title_draw_rect, R.layout.practice_rect),
        PageModel(R.layout.sample_point, R.string.title_draw_point, R.layout.practice_point),
        PageModel(R.layout.sample_oval, R.string.title_draw_oval, R.layout.practice_oval),
        PageModel(R.layout.sample_line, R.string.title_draw_line, R.layout.practice_line),
        PageModel(R.layout.sample_round_rect, R.string.title_draw_round_rect, R.layout.practice_round_rect),
        PageModel(R.layout.sample_arc, R.string.title_draw_arc, R.layout.practice_arc),
        PageModel(R.layout.sample_path, R.string.title_draw_path, R.layout.practice_path),
        PageModel(R.layout.sample_histogram, R.string.title_draw_histogram, R.layout.practice_histogram),
        PageModel(R.layout.sample_pie_chart, R.string.title_draw_pie_chart, R.layout.practice_pie_chart),
    )
    //@formatter:on

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPractice1MainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.viewPager.adapter = VpPageAdapter(supportFragmentManager, lifecycle,pageModels)
        TabLayoutMediator(binding.tabLayout,binding.viewPager) { tab, position ->
            tab.text = getString(pageModels[position].titleRes)
        }.attach()
    }

    class VpPageAdapter(
        fragmentManager: FragmentManager,
        lifecycle: Lifecycle,
        private val pageModels: List<PageModel> = listOf(),
    ) : FragmentStateAdapter(fragmentManager, lifecycle) {
        override fun createFragment(position: Int): Fragment {
            val model = pageModels[position]
            return PageFragment.newInstance(model.sampleLayoutRes, model.practiceLayoutRes)
        }

        override fun getItemCount(): Int {
            return pageModels.size
        }
    }

    data class PageModel(
        @LayoutRes val sampleLayoutRes: Int,
        @StringRes val titleRes: Int,
        @LayoutRes val practiceLayoutRes: Int,
    )

    companion object {
        fun start(activity: Context) {
            activity.startActivity(Intent(activity, Practice1MainActivity::class.java))
        }
    }
}